$(document).ready(function () {
    let url = 'ws://' + location.host + '/ws';
    let ws = new WebSocket(url);
    ws.onmessage = function (event) {
        let msg = JSON.parse(event.data);
        let node = $(msg.html);
        let msg_type = msg.type;

        switch (msg_type) {
            case 'user':

                node.hide();
                $("#user_list").html(node);
                node.slideDown();
                break;
            case 'sys':
            case 'chat':

                node.hide();
                $("#msg_log").append(node);
                node.slideDown();
                break;
            default:
                console.log(msg)
        }
    };
    $("#send").click(function () {
        let msg = {"username": "", "content": ""};
        msg["content"] = $("#msg").val();
        ws.send(JSON.stringify(msg));
        $("#msg").val("")
    });
    $("#msg").on('keypress', function (e) {
        if (e.keyCode == '13')
        {
            let msg = {"username": "", "content": ""};
            msg["content"] = $("#msg").val();
            ws.send(JSON.stringify(msg));
            $("#msg").val("")
        }
    })
});